#include "AppDelegate.h"
#include "UIMainMenu.h"

MMBigButton* MMBigButton::create(float width, string iconfile, string labelstr)
{
	MMBigButton* mmbb = new MMBigButton();
	mmbb->initWithColor(Color4B(255, 255, 255, 0));

	auto icon = Sprite::create(iconfile);
	UI::setNodeAnch(icon, ANCH_MB);
	icon->setScale(width / icon->getContentSize().width);
	icon->setOpacity(128);
	icon->setName("icon");
	mmbb->Icon = icon;

	TTFConfig labelStyle;
	labelStyle.fontFilePath = FONTA;
	labelStyle.fontSize = 32;
	labelStyle.distanceFieldEnabled = false;
	labelStyle.glyphs = GlyphCollection::DYNAMIC;
	labelStyle.customGlyphs = nullptr;
	labelStyle.outlineSize = 0;

	auto label = Label::createWithTTF(labelStyle, labelstr);
	UI::setNodeAnch(label, ANCH_MB);
	label->setOpacity(128);
	label->setScale(width*0.3 / label->getContentSize().height);
	label->setName("label");
	mmbb->Label = label;

	auto hoverUnderline = LayerColor::create(Color4B::WHITE,
											 width,
											 4);
	UI::setNodeAnch(hoverUnderline, ANCH_MB);
	hoverUnderline->setName("underline");
	mmbb->Hoverunderline = hoverUnderline;

	auto hover = Sprite::create("image/itemHover.png");
	UI::setNodeAnch(hover, ANCH_MB);
	hover->setName("hover");
	hover->setScaleX(width / hover->getContentSize().width);
	hover->setScaleY(0);
	hover->setTag(0);
	mmbb->Hover = hover;

	auto offsetx = width / 2;
	icon->setPosition(offsetx, hoverUnderline->getContentSize().height*hoverUnderline->getScaleY() + label->getContentSize().height*label->getScaleY());
	label->setPosition(offsetx, hoverUnderline->getContentSize().height*hoverUnderline->getScaleY());
	hoverUnderline->setPosition(offsetx, 0);
	hover->setPosition(offsetx, 0);
	mmbb->setContentSize(Size(width, icon->getPositionY() + icon->getContentSize().height*icon->getScaleY()));


	mmbb->addChild(icon, 45);
	mmbb->addChild(label, 46);
	mmbb->addChild(hover, 40);
	mmbb->addChild(hoverUnderline, 41);

	return mmbb;
}

void MMBigButton::onMouseMove(Event * e)
{
	auto tag = getTag();
	auto cond = mouseStatus == MOUSEHOVER || mouseStatus == MOUSEENTER;

	if (cond && !(tag % 10))
	{
		Hover->runAction(ScaleTo::create(0.15f, Hover->getScaleX(), Hover->getScaleX()));
		Hover->runAction(TintTo::create(0.15f, pUI->themeColor3B()));
		Hoverunderline->runAction(TintTo::create(0.15f, pUI->themeColor3B()));
		Icon->runAction(FadeTo::create(0.15f, 255));
		Label->runAction(FadeTo::create(0.15f, 255));
		setTag(tag / 10 * 10 + 1);
	}
	else if (!cond && tag % 10)
	{
		Hover->runAction(ScaleTo::create(0.15f, Hover->getScaleX(), 0));
		Hover->runAction(TintTo::create(0.15f, Color3B::WHITE));
		Hoverunderline->runAction(TintTo::create(0.15f, Color3B::WHITE));
		Icon->runAction(FadeTo::create(0.15f, 128));
		Label->runAction(FadeTo::create(0.15f, 128));
		setTag(tag / 10 * 10);
	}
	runAdditionalEventAction(MOUSEMOVE);
}

void MMBigButton::onMouseDown(Event * e)
{
	if (mouseStatus == MOUSEHOVER && mouseKey == MOUSELEFT)
	{
		runAdditionalEventAction(MOUSEDOWN);
	}
}

MMBottomBar * MMBottomBar::create(float height, bool isTitle)
{
	MMBottomBar* clip = new MMBottomBar();
	clip->setClippingRegion(Rect(0, 0, frameSize.width, height));

	auto pPSEmitter = ParticleSystemQuad::createWithTotalParticles(1000);
	pPSEmitter->setTexture(Director::getInstance()->getTextureCache()->addImage("image/square.png"));
	pPSEmitter->setAnchorPoint(Point(0, 0));
	pPSEmitter->setDuration(-1);
	pPSEmitter->setEmitterMode(ParticleSystem::Mode::GRAVITY);
	pPSEmitter->setGravity(Point(0, 0));
	pPSEmitter->setPosition(Point(frameSize.width*0.625, -80));
	pPSEmitter->setPosVar(Point(frameSize.width*0.625, 0));
	pPSEmitter->setLife(4);
	pPSEmitter->setLifeVar(2);
	pPSEmitter->setStartColor(pUI->themeColorF(128));
	pPSEmitter->setStartColorVar(Color4F(0, 0, 0, .4f));
	pPSEmitter->setEndColor(pUI->themeColorF(0));
	pPSEmitter->setEndColorVar(Color4F(0, 0, 0, 0));
	pPSEmitter->setStartSize(50);
	pPSEmitter->setStartSizeVar(30);
	pPSEmitter->setEndSize(50);
	pPSEmitter->setEndSizeVar(30);
	pPSEmitter->setAngle(135);
	pPSEmitter->setAngleVar(15);
	pPSEmitter->setSpeed(100);
	pPSEmitter->setSpeedVar(15);
	pPSEmitter->setStartSpin(0.0);
	pPSEmitter->setStartSpinVar(30.0);
	pPSEmitter->setEndSpin(270.0);
	pPSEmitter->setEndSpinVar(180.0);
	pPSEmitter->setEmissionRate(30);
	clip->addChild(pPSEmitter);
	if (isTitle)
	{
		pPSEmitter->setRotation(180);
		pPSEmitter->setPosition(frameSize.width*0.375, height + 80);
	}

	auto grad = LayerGradient::create(pUI->themeColorB(200), pUI->themeColorB(96), Vec2(-1, 1));
	grad->changeHeight(height);
	clip->addChild(grad);
	if (isTitle) grad->setRotation(180);

	auto line = DrawNode::create();
	if (isTitle) line->drawLine(Vec2(0, 1), Vec2(frameSize.width, 1), pUI->themeColorF(255));
	else line->drawLine(Vec2(0, height - 1), Vec2(frameSize.width, height - 1), pUI->themeColorF(255));
	clip->addChild(line);

	TTFConfig labelStyleTitle;
	labelStyleTitle.fontFilePath = FONTB;
	labelStyleTitle.fontSize = 32;
	labelStyleTitle.distanceFieldEnabled = false;
	labelStyleTitle.glyphs = GlyphCollection::DYNAMIC;
	labelStyleTitle.customGlyphs = nullptr;
	labelStyleTitle.outlineSize = 0;

	TTFConfig labelStyleArtist = labelStyleTitle;
	labelStyleArtist.fontSize = 16;

	auto lbTitle = Label::createWithTTF(labelStyleTitle, "");
	if (isTitle)
	{
		UI::setNodeAnch(lbTitle, ANCH_TL);
		lbTitle->setPosition(frameSize.width*0.1, height / 2);
	}
	else
	{
		UI::setNodeAnch(lbTitle, ANCH_BR);
		lbTitle->setPosition(frameSize.width*0.9, height / 2);
	}
	lbTitle->setTextColor(pUI->textColorB(255));
	clip->bgmTitle = lbTitle;
	clip->addChild(clip->bgmTitle);

	auto lbArtist = Label::createWithTTF(labelStyleArtist, "");
	if (isTitle)
	{
		UI::setNodeAnch(lbArtist, ANCH_BL);
		lbArtist->setPosition(frameSize.width*0.1, height / 2);
	}
	else
	{
		UI::setNodeAnch(lbArtist, ANCH_TR);
		lbArtist->setPosition(frameSize.width*0.9, height / 2);
	}
	lbArtist->setTextColor(pUI->textColorB(255));
	clip->bgmArtist = lbArtist;
	clip->addChild(clip->bgmArtist);

	return clip;
}

void MMBottomBar::setBgmInfo(string title, string artist)
{
	bgmTitle->setString(title);
	bgmArtist->setString(artist);
}
